package com.situ.sal.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.login.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.situ.sal.service.SalChanceService;
import com.situ.sal.entity.SalChance;
import com.situ.util.*;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * SalChance的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class SalChanceController {

	/** SalChanceService服务 */
	@Autowired
	private SalChanceService salChanceService;
	
	/**
	 * 查询所有SalChance数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "querySalChance", produces = {"application/json;charset=UTF-8"})
	public ResponseData find(SalChance value) {

		return ResponseData.ok().putDataValue("list",salChanceService.find(value));
	}
	
	/**
	 * 通过id查询SalChance数据的方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "querySalChanceById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData findOne(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",salChanceService.findOne(id));
	}
	
	/**
	 * 插入SalChance属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveSalChance")
	public ResponseData save(SalChance value) {
		return ResponseData.ok().putDataValue("obj", salChanceService.saveNotNull(value));
	}
	
	/**
	 * 更新SalChance属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateSalChance", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(SalChance value) {
		return ResponseData.ok().putDataValue("obj",salChanceService.updateNotNullById(value));
	}

	/**
	 * 通过id删除SalChance数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delSalChanceById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delete(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",salChanceService.deleteById(id));
	}


	//高级分页模糊查询
	@RequestMapping(value = "querySalChanceByLike", produces = {"application/json;charset=UTF-8"})
	public ResponseData sce(SalChance value,
							  @RequestParam(defaultValue ="1",required = false) int startPage ,
							  @RequestParam(defaultValue = "5",required = false) int PageSize) {

		PageHelper.startPage(startPage,PageSize );
		List<SalChance> list = salChanceService.queryByLike(value);
		PageInfo pageInfo=new PageInfo(list,5);


		return ResponseData.ok().putDataValue("pageInfo",pageInfo);
	}

	//高级分页模糊查询
	@RequestMapping(value = "querySalChanceByLike2", produces = {"application/json;charset=UTF-8"})
	public ResponseData sce2(SalChance value,
							@RequestParam(defaultValue ="1",required = false) int startPage ,
							@RequestParam(defaultValue = "5",required = false) int PageSize) {

		PageHelper.startPage(startPage,PageSize );
		List<SalChance> list = salChanceService.queryByLike2(value);
		PageInfo pageInfo=new PageInfo(list,5);


		return ResponseData.ok().putDataValue("pageInfo",pageInfo);
	}
	//删除
	@RequestMapping("deleteSalChance/{id}")
    public ResponseData delsc(@PathVariable int id){
	    salChanceService.delSalPlanById(id);
        salChanceService.deleteSalChanceById(id);
        return  ResponseData.ok();
    }

    //添加
    @RequestMapping("/addSC")
	public ResponseData addSal(SalChance sc){

		salChanceService.addSalChance(sc);

		return  ResponseData.ok();
	}

	//修改
	@RequestMapping("/updateSC")
	public ResponseData updateSalChance(SalChance sc){

		salChanceService.updateSalChance(sc);

		return ResponseData.ok();
	}

	//根据id查询
	@GetMapping("/querySalById/{id}")
	public ResponseData queryById(@PathVariable int id){

		SalChance salchance=salChanceService.selectSalChanceById(id);

		return ResponseData.ok().putDataValue("item", salchance);
	}

	//查询行数
	@GetMapping("/getCount/{id}")
	public ResponseData queryRow(@PathVariable int id){

		int row=salChanceService.getCount(id);

		return ResponseData.ok().putDataValue("b", row);
	}

	//根据id查询创建用户对象
	@GetMapping("/queryUersById")
	public ResponseData queryById2(int id){

		SysUser user=salChanceService.queryusersById(id);

		return ResponseData.ok().putDataValue("username",user.getUsr_Name());
	}
	//根据id查询指派用户对象
	@GetMapping("/queryUersById2")
	public ResponseData queryById3(int id){

		SysUser user=salChanceService.queryusersById(id);

		return ResponseData.ok().putDataValue("user",user.getUsr_Name());
	}

	//查询客户经理的用户信息
	@GetMapping("/queryByLikeU")
	public ResponseData queryById3(){

		List<SysUser> list=salChanceService.queryusers();

		return ResponseData.ok().putDataValue("s",list);
	}

	//修改指派
	@GetMapping("/updateZp")
	public ResponseData updateZp(SalChance s){

		s.setChc_status("2");
		salChanceService.updateSalChanceById1(s);

		return ResponseData.ok();
	}

	//时间转换器
	@InitBinder  //
	public void dateconvert(WebDataBinder bind){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PropertyEditor n=new CustomDateEditor(format, true);
		bind.registerCustomEditor(Date.class, n);
	}
}
